<?php

namespace app\user\controller;

use app\common\controller\User;
use think\Db;

class Msg extends User
{

    //修改资料
    public function index()
    {
        $db = model('Msg');
        $list = $db->where(['uid' => $this->uid])
            ->order('id DESC')
            ->paginate(9, $this->isMobile() ? true : false, $this->request->param());

        $this->assign('list', $list);
        $this->assign('page', $list->render());

        $this->assign('unReadCount', $db->where(array('uid' => $this->uid, 'status' => 0))->count());
        $this->assign('readCount', $db->where(array('uid' => $this->uid, 'status' => 1))->count());
        $this->assign('totalCount', $db->where(array('uid' => $this->uid))->count());

        return $this->fetch();
    }

    /**
     * 删除
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        $ids = $this->request->post('ids');
        $flag = Db::name('user_msg')->where(array('id' => array('IN', $ids)))->delete();
        if ($flag) {
            $this->result('', 1, '删除成功');
        } else {
            $this->result('', 1, '删除失败');
        }
    }

    /**
     * 设置状态
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function read()
    {

        $ids = $this->request->post('ids');
        $flag = Db::name('user_msg')->where(array('id' => array('IN', $ids)))->update(array('status' => 1));
        if ($flag) {
            return json(array('code' => 1, 'msg' => '设置已读成功！', 'data' => null));
        } else {
            return json(array('code' => 0, 'msg' => '设置已读失败！', 'data' => null));
        }

    }
}
